package service;

import entity.AdminDo;
import util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class AdminServiceImpl implements AdminService{
    @Override
    public String validateAdmin(AdminDo adminDo) {
        String uname = adminDo.getUserName();
        String sql="select userName from useraccounts where Username='"+uname+"'";//这个是查找是否存在用户
        System.out.println("输出用于查找用户的用户名的sql语句："+sql);
        Connection conn =null;
        PreparedStatement ps=null;
        ResultSet resultSet=null;
        try{
            conn=DBUtil.getConn();
            if(conn==null){
                return "数据库连接失败！";
            }
            ps=conn.prepareStatement(sql);
            resultSet = ps.executeQuery();//
            resultSet.next();
            /*
            while (resultSet.next()) {
                int id = resultSet.getInt("column_name");
                String name = resultSet.getString("column_name");
                // 其他类型的数据获取方法

                // 处理当前行数据
                // TODO: 根据需求进行处理

                // 打印当前行数据示例
                System.out.println("ID: " + id);
                System.out.println("Name: " + name);
                // 打印其他列的数据
            }

             */
            try {
                String u=resultSet.getString("Username");
            }catch (Exception e){
                System.out.println("该用户不存在");
                return "该用户不存在！";
            }
            sql="select * from useraccounts where Username='"+uname+"'";//查找用户的密码
            System.out.println("输出用于查找用户的密码的sql语句："+sql);
            ps=conn.prepareStatement(sql);
            resultSet = ps.executeQuery();
            resultSet.next();
            String pwd=resultSet.getString("Password");
            String UserType=resultSet.getString("UserType");
            System.out.println(UserType);
            if(adminDo.getPwd().equals(pwd)){
                return UserType;
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            DBUtil.closeConn(conn);
            DBUtil.closePS(ps);
            DBUtil.closeRs(resultSet);
        }
        return "false";
    }

    @Override
    public boolean enroll(AdminDo adminDo) {
        StringBuilder sql=new StringBuilder();
        sql.append("INSERT INTO useraccounts (Username, password , UserType) VALUES (? , ? , ?)");
        Connection connection=null;
        PreparedStatement ps=null;
        try{
            connection=DBUtil.getConn();
            ps = connection.prepareStatement(sql.toString());
            ps.setString(1, adminDo.getUserName());
            ps.setString(2, adminDo.getPwd());
            ps.setString(3, adminDo.getUserType());

            System.out.println(sql);
            return ps.executeUpdate()==1;//修改了一条记录，就说明新管理员插入成功
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DBUtil.closePS(ps);
            DBUtil.closeConn(connection);
        }
        return false;
    }

}
